<template>
    <div class="login">
      <!-- 头部 -->
      <van-nav-bar
        title="用户登录"
        left-arrow
        @click-left="$router.go(-1)"
      />
      <!-- 验证 -->
      <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>
      <div class="form">
        <div class="form-item">
          <input class="inp"
                v-model="phoneNumber"
                maxlength="11"
                placeholder="请输入手机号码"
                type="text"
                >
        </div>
        <div class="form-item">
          <input class="inp"
                v-model="picCode"
                maxlength="4"
                placeholder="请输入图形验证码"
                type="text"
                >
          <img v-if="picurl" :src="picurl" alt="" @click="getCode">
        </div>
        <div class="form-item">
          <input class="inp"
                v-model="msgCode"
                placeholder="请输入短信验证码"
                type="text">
          <button @click="sendCode">
            {{second === totalSecond? "获取验证码" : second + "秒后重新发送"}}
          </button>
        </div>
      </div>
      <div class="login-btn" @click="login">登录</div>
      </div>
    </div>
  </template>

<script>
import { getPicCode, getSmsCode, login } from '@/api/login'
import { Toast } from 'vant'

export default {
  name: 'LoginIndex',
  data () {
    return {
      phoneNumber: '', // 用户输入的手机号
      picCode: '', // 用户输入的图形验证码
      pickey: '',
      picurl: '',
      msgCode: '', // 用户输入的短信验证码
      totalSecond: 10, // 定时器总秒数
      second: 10, // 开定时器对seccond--，当second为0时，清除定时器
      timer: null,
      smsCode: '246810' // 后台返回的短信验证码
    }
  },
  async created () {
    this.getCode()
  },
  destroyed () {
    // 清除定时器
    clearInterval(this.timer)
  },
  methods: {
    // 获取图形验证码
    async getCode () {
      const { data: { base64, key } } = await getPicCode()
      this.picurl = base64
      this.pickey = key
    },
    // 格式校验
    validFn () {
      if (!/^1[3-9]\d{9}$/.test(this.phoneNumber)) {
        this.$toast('请输入正确的手机号')
        return
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        this.$toast('请输入正确的图形验证码')
        return
      }
      return true
    },
    // 发送短信验证码
    async sendCode () {
      if (!this.validFn()) return
      const res = await getSmsCode(this.picCode, this.pickey, this.phoneNumber)
      console.log(res)
      Toast(res.message)
      // 节流，防止用户多次点击
      if (this.timer !== null) return
      // 实现倒计时
      this.timer = setInterval(() => {
        this.second--
        if (this.second === 0) {
          clearInterval(this.timer)
          this.timer = null
          this.second = this.totalSecond
        }
      }, 1000)
    },
    // 校验短信验证码
    validSmsCode () {
      if (this.smsCode !== this.msgCode) {
        this.$toast('请输入正确的短信验证码')
        return false
      }
      return true
    },
    async login () {
      if (!this.validFn()) return
      if (!this.validSmsCode()) return
      const res = await login(this.phoneNumber, this.msgCode)
      this.$store.commit('user/setUserInfo', res.data)
      this.$toast.success('登录成功')
      // 登录成功后，回跳到上一个页面
      const url = this.$route.query.redirect || '/home'
      this.$router.replace(url)
    }
  }
}
</script>

<style lang="less">
.container{
  padding: 49px 29px;
  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }
  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
